package com.example.demo.exception;


import com.example.demo.commen.AppResult;
import com.example.demo.commen.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandle {
    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult applicationHandler(ApplicationException e){
        log.info(e.getMessage());

        //非空检验
        if(e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES.getMessage());
        }
        return AppResult.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult exceptionHandler(Exception e){
        log.info("捕获异常");
        log.info(e.getMessage());

        if(e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES.getMessage());
        }
        System.out.println("1111222222");
        return AppResult.failed(e.getMessage());
    }
}
